class RoutesController < ApplicationController

	def index
		@routes = Route.find(:all)
	end

	def new
		@route = Route.new
	end

	def create
		@route = Route.new(params[:route])
		return self.unauthorized unless self.current_user.can('create:route', @route)
		if @route.save
			flash[:notice] = 'New route successfully added.'
			redirect_to(routes_path)
		else
			render :action => "new"
		end
	end

	def destroy
		@route = Route.find(params[:id])
		return self.unauthorized unless self.current_user.can('delete:route', @route)
		@route.destroy

		redirect_to routes_path
	end

	def edit
		@route = Route.find(params[:id])
		return self.unauthorized unless self.current_user.can('edit:route', @route)
		add_page_token(@route.name)
	end

	def show
		@route = Route.find(params[:id])
		add_page_token(@route.name)
	end

	def defaults
		@defaultroutes = Routesite.defaults
		return self.unauthorized unless self.current_user.can('reorder:route', nil)
		if request.post?
			@defaultroutes.each do |r|
				r.updateSiteRoute(-1,params['route'+r.id.to_s])
			end
			# reload
			@defaultroutes = Routesite.defaults
			flash[:notice] = "Default route order successfully changed."
		end
	end
  
	def update 
		@route = Route.find(params[:id])
		return self.unauthorized unless self.current_user.can('edit:route', @route)
		if @route.update_attributes(params[:route])
			flash[:notice] = 'The route was successfully updated.'
			redirect_to(routes_path)
		else
			render :action => "edit"
		end
		add_page_token(@route.name)
	  end 
end
